//
//  CHStringExtensions.h
//  POBase
//
//  Created by Pieter Omvlee on 3/9/08.
//  Copyright 2008 Bohemian Coding. All rights reserved.
//

#import "CHSheetController.h"

@implementation CHSheetController

@synthesize object;
@synthesize callbackObject;
@synthesize callback;
@synthesize delegate;

+ (id)runForWindow:(NSWindow *)aWindow
{
  if ([aWindow attachedSheet]) return nil;
  CHSheetController *modalSheet = [[self alloc] _init];
  
  modalSheet.callbackObject = nil;
  
  [NSApp beginSheet:[modalSheet window]
     modalForWindow:aWindow
      modalDelegate:modalSheet
     didEndSelector:@selector(_sheetDidEnd:returnCode:contextInfo:)
        contextInfo:nil];
  [[modalSheet window] makeKeyAndOrderFront:nil]; // redundant but cleaner
  return modalSheet;
}

+ (id)runForWindow:(NSWindow *)aWindow withObject:(id)obj delegate:(id)del
{
  if ([aWindow attachedSheet]) return nil;
  CHSheetController *modalSheet = [[self alloc] _init];
  
  modalSheet.object   = obj;
  modalSheet.delegate = del;
  
  [NSApp beginSheet:[modalSheet window]
     modalForWindow:aWindow
      modalDelegate:modalSheet
     didEndSelector:@selector(_sheetDidEnd:returnCode:contextInfo:)
        contextInfo:nil];
  [[modalSheet window] makeKeyAndOrderFront:nil]; // redundant but cleaner
  return modalSheet;
}

+ (id)runForWindow:(NSWindow *)aWindow withObject:(id)obj
{
  if ([aWindow attachedSheet]) return nil;
  CHSheetController *modalSheet = [[self alloc] _init];
  
  modalSheet.object = obj;
  modalSheet.callbackObject = nil;
  
  [NSApp beginSheet:[modalSheet window]
     modalForWindow:aWindow
      modalDelegate:modalSheet
     didEndSelector:@selector(_sheetDidEnd:returnCode:contextInfo:)
        contextInfo:nil];
  [[modalSheet window] makeKeyAndOrderFront:nil]; // redundant but cleaner
  return modalSheet;
}

+ (id)runForWindow:(NSWindow *)aWindow callback:(SEL)sel onObject:(id)anObject
{
  if ([aWindow attachedSheet]) return nil;
  CHSheetController *modalSheet = [[self alloc] _init];
  
  modalSheet.callback = sel;
  modalSheet.callbackObject = anObject;
  
  [NSApp beginSheet:[modalSheet window]
     modalForWindow:aWindow
      modalDelegate:modalSheet
     didEndSelector:@selector(_sheetDidEnd:returnCode:contextInfo:)
        contextInfo:nil];
  [[modalSheet window] makeKeyAndOrderFront:nil]; // redundant but cleaner
  return modalSheet;
}

- (IBAction)confirm:(id)sender
{
  if (callbackObject)
    [callbackObject performSelector:callback withObject:[self infoObject]];
  
  if (delegate)
    [delegate sheetControllerDidEnd:self];
  
  [self cancel:sender];
}

- (IBAction)cancel:(id)sender
{
  [NSApp endSheet:[self window] returnCode:0]; // stop the app's modality
}

- (id)infoObject
{
  return self;
}

- (id)_init
{
  self = [self initWithWindowNibName:NSStringFromClass([self class])];
  if (self) {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(cancel:)
                   name:@"MSCloseAllSheets"
                 object:nil];
  }
  return self;
}

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [object release];
  [callbackObject release];
  callbackObject = nil;
  [super dealloc];
}

// private callbacks

- (void)_sheetDidEnd:(NSWindow*)sheetWindow
          returnCode:(int)returnCode
         contextInfo:(void*)contextInfo;
{
  [sheetWindow orderOut:nil]; // hide sheet
  [self autorelease];
}

@end